package studychinese.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.activity.MyApplication;
import com.jaeger.library.StatusBarUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import studychinese.callback.DialogCallback;
import studychinese.config.AppUrl;
import studychinese.config.ConFigTag;
import studychinese.config.Url;
import studychinese.model.RegisterBean;
import studychinese.model.UserBean;
import studychinese.utils.AppUtils;
import studychinese.utils.SPUtils;
import studychinese.utils.ToastUtils;
import studychinese.utils.VerifyUtils;

public class LoginActivity extends BaseAcitivity {


    @Bind(R.id.tv_login_text)
    TextView tvLoginText;
    @Bind(R.id.et_login_account)
    EditText etLoginAccount;
    @Bind(R.id.et_login_psd)
    EditText etLoginPsd;
    @Bind(R.id.iv_login_pw_cli)
    ImageView ivLoginPwCli;
    @Bind(R.id.iv_login_login)
    ImageView iv_login_login;
    @Bind(R.id.iv_login_colse)
    TextView iv_login_colse;
    @Bind(R.id.rl_login_back)
    RelativeLayout rlLoginBack;
    @Bind(R.id.btn_login_goregister)
    Button btnLoginGoregister;
    @Bind(R.id.tv_login_fogot_psd)
    TextView tvLoginFogotPsd;
    @Bind(R.id.tv_login_point)
    TextView tv_login_point;
    @Bind(R.id.tv_login_point2)
    TextView tv_login_point2;
    @Bind(R.id.ll_back)
    LinearLayout ll_back;
    private Boolean showPassword = true;
    private String account;
    private String pwd;
    private SharedPreferences.Editor edit;
    private SharedPreferences splogindate;

    @Override
    protected int setContainer() {
        splogindate = getSharedPreferences("Date", MODE_PRIVATE);
        edit = splogindate.edit();

        return R.layout.activity_login1;
    }

    @Override
    protected void setStatusBar() {
        StatusBarUtil.setTranslucentForImageView(this, 0, ll_back);
    }

    @Override
    public void innitView() {
        ButterKnife.bind(this);


        setIsFocusabale(false);
        etLoginAccount.addTextChangedListener(textWatcher);
        etLoginPsd.addTextChangedListener(textWatcher);
        etLoginAccount.setOnFocusChangeListener(new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    // 此处为失去焦点时的处理内容
                    if (!VerifyUtils.isEmailNO(etLoginAccount.getText().toString().trim())) {
                        tv_login_point.setVisibility(View.VISIBLE);

                    } else {
                        tv_login_point.setVisibility(View.GONE);

                    }

                }
            }
        });
        etLoginPsd.setOnFocusChangeListener(new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    // 此处为失去焦点时的处理内容
                    if (etLoginPsd.getText().length() < 6) {
                        tv_login_point2.setVisibility(View.VISIBLE);

                    } else {
                        tv_login_point2.setVisibility(View.GONE);

                    }

                }
            }
        });
        String input = getIntent().getStringExtra("input");
        if (input.equals("zhengchang")) {
            iv_login_colse.setVisibility(View.VISIBLE);

        } else {
            iv_login_colse.setVisibility(View.INVISIBLE);
        }

    }

    private void setIsFocusabale(boolean b) {

        if (b) {
            iv_login_login.setFocusable(true);
            iv_login_login.setImageResource(R.mipmap.login_enter_click);
        } else {
            iv_login_login.setFocusable(false);
            iv_login_login.setImageResource(R.mipmap.login_enter_unclick);
        }
    }


    @OnClick({R.id.iv_login_colse, R.id.iv_login_pw_cli, R.id.btn_login_goregister, R.id.tv_login_fogot_psd, R.id.iv_login_login, R.id.tv_selete_language})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_login_colse:
                iv_login_colse.setVisibility(View.VISIBLE);
                RegisterBean.DataBean dataBean = new RegisterBean.DataBean();
                UserBean.setPerson(dataBean);
                UserBean.putUserBean(dataBean);
                Intent intent1 = new Intent(this, WhatStudyActivity.class);
                startActivity(intent1);
                break;
            case R.id.iv_login_pw_cli:
                if (showPassword) {// 显示密码
                    ivLoginPwCli.setImageDrawable(getResources().getDrawable(R.mipmap.login_showp_click));
                    etLoginPsd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    etLoginPsd.setSelection(etLoginPsd.getText().toString().length());
                    showPassword = !showPassword;
                } else {// 隐藏密码
                    ivLoginPwCli.setImageDrawable(getResources().getDrawable(R.mipmap.login_unshowp_click));
                    etLoginPsd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    etLoginPsd.setSelection(etLoginPsd.getText().toString().length());
                    showPassword = !showPassword;
                }
                break;
            case R.id.btn_login_goregister:
                String input = getIntent().getStringExtra("input");
                if (input.equals("zhengchang")) {
                    RegisterBean.DataBean dataBean1 = new RegisterBean.DataBean();
                    UserBean.setPerson(dataBean1);
                    UserBean.putUserBean(dataBean1);
                }
                Intent intent = new Intent(this, RegisterActivity.class);
                intent.putExtra("input", input);
                startActivity(intent);
                break;
            case R.id.tv_login_fogot_psd:

                break;
            case R.id.tv_selete_language:
                startActivity(new Intent(this, SetLangue.class));
                this.finish();
                break;
            case R.id.iv_login_login:

                account = etLoginAccount.getText().toString().trim();
                pwd = etLoginPsd.getText().toString().trim();
                if ("".equals(account)) {
                    ToastUtils.showShort("账号不能为空");
                } else if ("".equals(pwd)) {
                    ToastUtils.showShort("账号不能为空");
                } else if (pwd.length() < 6) {
                    tv_login_point2.setVisibility(View.VISIBLE);
                } else {
                    setData();
                }
                break;
        }
    }


    private void setData() {
        String user_name = etLoginAccount.getText().toString();
        String user_passwd = etLoginPsd.getText().toString();
        OkGo.<String>post(AppUrl.getAppUrl(Url.REGISTER_USERLOGIN)).params("user_name", user_name).params("user_passwd", user_passwd).execute(new DialogCallback<String>(this, true, "正在登陆。。。") {
            @Override
            public void onSuccess(Response<String> response) {
                RegisterBean registerBean = JSON.parseObject(response.body(), RegisterBean.class);
                if (registerBean.getState().equals(ConFigTag.state)) {
                    RegisterBean.DataBean data = registerBean.getData();
                    SPUtils.putObject(MyApplication.getInstance(), "userInfo", "userInfo", data);
                    AppUtils.changeLogin(LoginActivity.this, true);
                    startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                    LoginActivity.this.finish();
                } else {
                    ToastUtils.showShort(registerBean.getMessage());
                }
            }
        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //写下你希望按下返回键达到的效果代码，不写则不会有反应
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }


    TextWatcher textWatcher = new TextWatcher() {
        // 输入文本之前的状态
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        // 输入文本中的状态
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {


        }

        // 输入文本之后的状态
        @Override
        public void afterTextChanged(Editable s) {

            if (etLoginPsd.getText().length() >= 6 && VerifyUtils.isEmailNO(etLoginAccount.getText().toString().trim())) {
                setIsFocusabale(true);
            } else {
                setIsFocusabale(false);
            }
        }
    };

}
